(ns btree.macros-test
	(:require [clojure.test :refer :all] [btree.macros :refer :all] [btree.operations :refer :all])
)

(deftest tree-from-pageList-test
	(testing "2 lvl tree created with macro is wrong!" (is (= 
		(tree-from-pageList 2 (
					   (10 50)
			(1 5 6) (15 35 45 48) (57 62 117)
		))
		{
			1 {:children [2 3 4], :parent nil, :keys [10 50]}, 
			2 {:children [], :parent 1, :keys [1 5 6]},
			3 {:children [], :parent 1, :keys [15 35 45 48]},
			4 {:children [], :parent 1, :keys [57 62 117]},
			:root 1, :pageSize 2, :nextId 5
		}
	)))
	
	(testing "3 lvl tree created with macro is wrong!" (is (= 
		(tree-from-pageList 2 (
											 (20 50)
				  (5 9) 					 (25 35) 				   (57 62)
			(1 3) (6 7) (10 13)   (21 22 23) (29 31) (40 45)   (55 56) (59 61) (63 64 70)
		))
		{
			1 {:children [2 3 4], :parent nil, :keys [20 50]}, 
			2 {:children [5 6 7], :parent 1, :keys [5 9]}, 
			3 {:children [8 9 10], :parent 1, :keys [25 35]}, 
			4 {:children [11 12 13], :parent 1, :keys [57 62]}, 
			5 {:children [], :parent 2, :keys [1 3]}, 
			6 {:children [], :parent 2, :keys [6 7]}, 
			7 {:children [], :parent 2, :keys [10 13]}, 
			8 {:children [], :parent 3, :keys [21 22 23]}, 
			9 {:children [], :parent 3, :keys [29 31]}, 
			10 {:children [], :parent 3, :keys [40 45]}, 
			11 {:children [], :parent 4, :keys [55 56]}, 
			12 {:children [], :parent 4, :keys [59 61]}, 
			13 {:children [], :parent 4, :keys [63 64 70]}, 
			:root 1, :pageSize 2, :nextId 14
		}
	)))
	
	(testing "2 lvl tree created with macro is wrong!" (is (= 
		(tree-from-pageList 2 (
					   [10 50]
			[1 5 6] [15 35 45 48] [57 62 117]
		))
		{
			1 {:children [2 3 4], :parent nil, :keys [10 50]}, 
			2 {:children [], :parent 1, :keys [1 5 6]},
			3 {:children [], :parent 1, :keys [15 35 45 48]},
			4 {:children [], :parent 1, :keys [57 62 117]},
			:root 1, :pageSize 2, :nextId 5
		}
	)))
	
)

(deftest tree-from-valueList-test
	(testing "tree created from list with macro is wrong!" (is (= (tree-from-valueList 2 1 2 3 4 5 6) {3 {:keys [3], :children [1 2], :parent nil}, 2 {:keys [4 5 6], :children [], :parent 3}, :pageSize 2, :root 3, :nextId 4, 1 {:keys [1 2], :children [], :parent 3}})))
)

(deftest tree-do-test
	(testing "tree created by executing list of commands is wrong!" (is (= (tree-do (
		(tree-create 2 3 2 8)
		(tree-add 1)
		(tree-add 12)
		(tree-remove 8)
		(tree-remove 7)
		(tree-add 8)
		(tree-add 0)
		(tree-remove 2)
	)) {5 {:keys [3], :children [1 4], :parent nil}, 4 {:keys [8 12], :children [], :parent 5}, :pageSize 2, :root 5, :nextId 6, 1 {:keys [0 1], :children [], :parent 5}})))
)